package cn.iocoder.yudao.module.blog.controller.admin.caselike;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.blog.controller.admin.caselike.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.caselike.BlogCaseLikeDO;
import cn.iocoder.yudao.module.blog.convert.caselike.BlogCaseLikeConvert;
import cn.iocoder.yudao.module.blog.service.caselike.BlogCaseLikeService;

@Tag(name = "管理后台 - 案例点赞")
@RestController
@RequestMapping("/blog/case-like")
@Validated
public class BlogCaseLikeController {

    @Resource
    private BlogCaseLikeService caseLikeService;

    @PostMapping("/create")
    @Operation(summary = "创建案例点赞")
    @PreAuthorize("@ss.hasPermission('blog:case-like:create')")
    public CommonResult<Long> createCaseLike(@Valid @RequestBody BlogCaseLikeCreateReqVO createReqVO) {
        return success(caseLikeService.createCaseLike(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新案例点赞")
    @PreAuthorize("@ss.hasPermission('blog:case-like:update')")
    public CommonResult<Boolean> updateCaseLike(@Valid @RequestBody BlogCaseLikeUpdateReqVO updateReqVO) {
        caseLikeService.updateCaseLike(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除案例点赞")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:case-like:delete')")
    public CommonResult<Boolean> deleteCaseLike(@RequestParam("id") Long id) {
        caseLikeService.deleteCaseLike(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得案例点赞")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:case-like:query')")
    public CommonResult<BlogCaseLikeRespVO> getCaseLike(@RequestParam("id") Long id) {
        BlogCaseLikeDO caseLike = caseLikeService.getCaseLike(id);
        return success(BlogCaseLikeConvert.INSTANCE.convert(caseLike));
    }

    @GetMapping("/list")
    @Operation(summary = "获得案例点赞列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blog:case-like:query')")
    public CommonResult<List<BlogCaseLikeRespVO>> getCaseLikeList(@RequestParam("ids") Collection<Long> ids) {
        List<BlogCaseLikeDO> list = caseLikeService.getCaseLikeList(ids);
        return success(BlogCaseLikeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得案例点赞分页")
    @PreAuthorize("@ss.hasPermission('blog:case-like:query')")
    public CommonResult<PageResult<BlogCaseLikeRespVO>> getCaseLikePage(@Valid BlogCaseLikePageReqVO pageVO) {
        PageResult<BlogCaseLikeDO> pageResult = caseLikeService.getCaseLikePage(pageVO);
        return success(BlogCaseLikeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出案例点赞 Excel")
    @PreAuthorize("@ss.hasPermission('blog:case-like:export')")
    @OperateLog(type = EXPORT)
    public void exportCaseLikeExcel(@Valid BlogCaseLikeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BlogCaseLikeDO> list = caseLikeService.getCaseLikeList(exportReqVO);
        // 导出 Excel
        List<BlogCaseLikeExcelVO> datas = BlogCaseLikeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "案例点赞.xls", "数据", BlogCaseLikeExcelVO.class, datas);
    }

}
